package  
{
	import flash.display.Sprite;
  import mge.Localization;
  import mge.StringUtils;
	
	/**
   * ...
   * @author ivan
   */
  public class LocParser extends Sprite 
  {
    
    [Embed(source = '../res/source_c.txt', mimeType = "application/octet-stream")]
    private const rSource:Class;
    
    private const LANGUAGE:String = "es";
    
    
    private var result:String = "";
    
    public function LocParser() 
    {      
      var source:String = new rSource;
      
      for (var i:int = 1; i < 100; i++)
      {
        var idx:int = source.indexOf("Sector" + i + " ");
        if (idx != -1)
        {
          var end:int = source.indexOf("Sector", idx +1);
          parseSector(source.substring(idx, end != -1 ? end : int.MAX_VALUE));
        }
      }
      
      var xml:XML = new XML("<localization>" + result + "</localization>");
      trace(xml);
    }
    
    private function parseSector(sector:String):void
    {
      var sectorNum:int = (int)(sector.substring(6, sector.indexOf(" ")));
      var name:String = sector.substring(sector.indexOf("screen:") + 8, sector.indexOf("\r"));
      
      result += '<dialog id="S' + sectorNum + '_NAME"> <mood>null</mood> <' + LANGUAGE + '><![CDATA[' + name + ']]></' + LANGUAGE + '> </dialog>'
      
      // Busca el briefing
      var briefingStart:int = sector.indexOf("Briefing:");
      var briefingEnd:int = sector.indexOf("Tip", briefingStart);
      
      var briefing:String = sector.substring(briefingStart, briefingEnd);
      briefing = briefing.substr(briefing.indexOf("\n") + 1);
      
      var lines:Array = briefing.split("//");
      
      for (var i:int = 0; i < lines.length-1; i++)
      {
        var line:String = lines[i];
        while (line.indexOf("\r") == 0)
          line = line.substr(2);
        result += '<dialog id="S' + sectorNum + '_BRIEF.' + i + '"> <mood>normal</mood> <' + LANGUAGE + '><![CDATA[' + line + ']]></' + LANGUAGE + '> </dialog>'
      }
      
      // Busca los tips
      parseTips(sector, sectorNum);
    }
    
    private function parseTips(sector:String, sectorNum:int):void
    {
      var tipStart:int = 0;
      tipStart = sector.indexOf("\nTip", tipStart);
      
      while (tipStart != -1)
      {
        var tipEnd:int = sector.indexOf(".", tipStart);
        var tipNumber:String = sector.substring(tipStart + 4, tipEnd);
        
        var tip:String = sector.substring(tipStart + 1 /* + 1 to avoid first \n */, sector.indexOf("\nTip", tipEnd));
        
        var mood:String = findMood(tip);
        
        tip = tip.substr(tip.indexOf("\n") + 1);
        
        tip = removeReturnCharactersAtEnd(tip);
        
        var lines:Array = tip.split("//");
        for (var i:int = 0; i < lines.length; i++)
        {
          var line:String = lines[i];
          
          while (line.indexOf("\r") == 0) // Quita enters de principio de linea
            line = line.substr(2);
            
          line = removeParentheses(line);
          
          result += '<dialog id="S' + sectorNum + '_TIP_' + tipNumber + '.' + i + '"> <mood>' + mood + '</mood> <' + LANGUAGE + '><![CDATA[' + line + ']]></' + LANGUAGE + '> </dialog>'
        }
        
        tipStart = sector.indexOf("\nTip", tipEnd);
        
        // Check end of tips.
        if (sector.indexOf("\nTipsEnd") == tipStart)
          tipStart = -1;
      }
    }
    
    private function removeParentheses(t:String):String
    {
      var start:int = t.indexOf("(");
      while (start != -1)
      {
        var end:int = t.indexOf(")", start);
        if (end != -1)
        {
          t = t.substr(0, start) + t.substring(end+1);
        }
        start = t.indexOf("(");
      }
      return t;
    }
    
    private function removeReturnCharactersAtEnd(text:String):String
    {
      var found:Boolean = true;
      
      while (found)
      {
        found = false;
        
        if (text.lastIndexOf("\r") != -1 && text.lastIndexOf("\r") >= text.length - 1)
        {
          text = text.substr(0, text.lastIndexOf("\r"));
          found = true;
        }
        
        if (text.lastIndexOf("\n") != -1 && text.lastIndexOf("\n") >= text.length - 1)
        {
          text = text.substr(0, text.lastIndexOf("\n"));
          found = true;
        }
      }
      
      return text;
    }
    
    private function findMood(s:String):String
    {
      for (var i:int = 0; i < Localization.MOODS.length; i++)
        if (s.indexOf("(" + Localization.MOODS[i] + ")") != -1)
          return Localization.MOODS[i];
          
      return "null";
    }
    
  }

}